package com.yc.hotel.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.hotel.dto.RoomInfoDTO;
import com.yc.hotel.dto.UserInfoDTO;
import com.yc.hotel.enums.ResultEnum;
import com.yc.hotel.room.feign.IRoomInfoFeignClient;
import com.yc.hotel.util.ConstantInfo;
import com.yc.hotel.vo.ResultVO;

@RestController
@RequestMapping("/room")
public class RoomInfoFeignController {

	@Autowired
	private IRoomInfoFeignClient roomInfoServiceImpl;

	
	@PostMapping("/add")
	public ResultVO add(RoomInfoDTO room, MultipartFile picss, MultipartFile photos, HttpSession session) {

		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		UserInfoDTO user = (UserInfoDTO)obj;
		int uno = user.getUno();
		String savePath = "";
		File dest = null;
		if (picss != null ) {
			String picStr = "";
			try {
				// 重命名图片
				savePath = ConstantInfo.UPLOADPATH + "/pics" + "/" + System.currentTimeMillis()+ "_"
						+ picss.getOriginalFilename();
				dest = new File(ConstantInfo.basePath, savePath);
				picss.transferTo(dest);
				
				picStr += savePath;
				
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			room.setPics(picStr);
		}
		if (photos != null ) {
			String picStr = "";
			try {
				// 重命名图片
				savePath = ConstantInfo.UPLOADPATH  + "/photo" + "/" + System.currentTimeMillis()+ "_"
						+ photos.getOriginalFilename();
				dest = new File(ConstantInfo.basePath, savePath);
				photos.transferTo(dest);
				
				picStr += savePath;
				
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			room.setPhoto(picStr);
		}
		room.setUno(uno);

		int result = roomInfoServiceImpl.add(room);
		return new ResultVO(ResultEnum.SUCCESS, result);
	}

	
	@PostMapping("/updateStatus")
	public ResultVO updateStatus(@RequestParam Map<String, Object> map){
		int result = roomInfoServiceImpl.updateStatus(map);
		return new ResultVO(ResultEnum.SUCCESS, result);
	};

	@PostMapping("/finds")
	private ResultVO finds(@RequestParam Map<String, Object> map) {
		
		List<RoomInfoDTO> list = roomInfoServiceImpl.finds(map);
		
		return new ResultVO(ResultEnum.SUCCESS, list);
	};

}
